module.exports = class extends think.cmswing.rest {
	
   /**
   * 获取分类信息
   * /api/agent 获取全部代理
   * /api/agent/1 获取代理id为1的栏目信息
   * @returns {Promise.<*>}
   */
	
  async getAction() {
     const cmd = this.get('cmd') || '';
     const id = this.get('id') || '';
     const qty = this.get('qty') || '';
     const pid = this.get('pid') || '';
     let order_amount;// 订单金额
     let real_amount;// 商品参与获得的价格
     // 应付金额
//     const parr = [];
//     const isgoods = [];
     let data = {};
     
     let goods = await this.model('document').find(id);
     let goodprice= get_price(goods.price, 1);
     let goods_price = formatCurrency(Number(goodprice)* Number(qty));
//     parr.push(goods_price);
     goods.goods_price=goods_price;
     goods.goods_nums=qty;
     goods.goods_no = await this.model('document_shop').where({id:id}).getField('goods_no', true);
     		
     let prom_goods = {};
     prom_goods.id=goods.id;
     prom_goods.uid=this.user.uid;
     prom_goods.product_id=goods.id;
     prom_goods.qty=qty;
     prom_goods.type=goods.type;
     prom_goods.price=goods_price;
     prom_goods.title=goods.title;
     prom_goods.unit_price=goods.goods_price;
     prom_goods.pic= await get_pic(goods.pics, 1, 100, 100);
     prom_goods.url=get_url(goods.name, goods.id);
     prom_goods.weight=0;
     prom_goods.fare='';
     goods.prom_goods = prom_goods;
//     isgoods.push(goods);
     
     real_amount = goods_price;
     // 用户
     data.user_id = pid;
     // 生成订单编号//todo
     data.order_no = await this.model('cmswing/order').orderid(pid);
     
     data.real_amount = real_amount;

     // 支付状态 pay_stayus 0:未付款 ,1:已付款
     data.pay_status = 0;
     // 订单状态 status 2:等待审核，3:已审核
     data.status = 2;
     // 发货状态 delivery_status 0:未发货，1:已发货
     data.delivery_status = 0;
     // 订单创建时间 create_time
     data.create_time = new Date().valueOf();
     // 客户订单备注
     order_amount = Number(data.real_amount);
     data.order_amount = order_amount;
     // 生成订单
     const order_id = await this.model('order').add(data);

     // 储存宝贝
     const newgoods = {};
     newgoods.order_id = order_id;
     newgoods.goods_id = goods.id;
     newgoods.goods_price = goods.goods_price;
     newgoods.goods_real_price = goods.goods_price;
     newgoods.goods_nums = goods.goods_nums;
     newgoods.prom_goods = JSON.stringify(goods.prom_goods);
     newgoods.goods_no = goods.goods_no;
     
     await this.model('order_goods').add(newgoods);
     
     if (this.isMobile) {
         return this.display(this.mtpl());
       } else {
         return this.display();
     }
 
     // 减少订单中商品的库存
     //await this.model('cmswing/order').stock(order_id, true);
     //return this.success({name: '订单创建成功，正在跳转支付页面！', url: `/center/pay/pay?order=${order_id}&setp=3`});
   
  }
  
  async postAction() {
	 let payment;
     let pay;
     let charges;
     const post = this.post();
     let data = post;
     // 获取订单信息
     const order = await this.model('order').where({pay_status: 0, user_id: this.user.uid}).find(post.order_id);
     if (think.isEmpty(order)) {
       return this.fail('订单不存在！');
     }
     // 更新订单的支付方式
     await this.model('order').where({id: order.id}).update({payment: post.payment});
     // 支付日志
     const receiving = {
       order_id: post.order_id,
       user_id: this.user.uid,
       amount: order.order_amount,
       create_time: new Date().getTime(),
       payment_time: new Date().getTime(),
       doc_type: 0,
       payment_id: post.payment,
       pay_status: 0
     };

     // 100预付款支付

     if (post.payment == 100) {
       // 先检查下用户余额
       const balance = await this.model('member').where({user_id: this.user.uid}).getField('amount', true);
       if (Number(balance) < Number(order.order_amount)) {
         return this.fail('您余额不足，请充值，或者选择其他支付方式！');
       } else {
         // 扣款
         const decrement = this.model('member').where({id:this.user.uid}).decrement('amount', Number(order.order_amount));
         if (decrement) {
           // 扣款成功改变订单状态
           await this.model('order').where({order_no: order.order_no}).update({status: 3, pay_status: 1});
           // 扣款成功后插入日志
           const log = {
             admin_id: 0,
             user_id: this.user.uid,
             type: 2,
             time: new Date().valueOf(),
             amount: (0 - Number(order.order_amount)),
             amount_log: await this.model('member').where({id: this.user.uid}).getField('amount', true),
             note: `${await get_nickname(this.user.uid)} 通过余额支付方式进行商品购买,订单编号：${order.order_no}`
           };
           await this.model('balance_log').add(log);
           receiving.pay_status = 1;
           await this.model('doc_receiving').add(receiving);
           const url = `/center/pay/payres/?c_o_id=${post.order_id}`;
           return this.success({name: '支付订单对接成功，正在转跳！', url: url});
         } else {
           return this.fail('您没有要支付的订单');
         }
       }
     }
     if (post.payment == 1001) {
       const url = `/center/pay/payres/?c_o_id=${post.order_id}`;
       return this.success({name: '支付订单对接成功，正在转跳！', url: url});
     }
     // 1001货到付款
     if (think.isEmpty(order)) {
       return this.fail('您没有要支付的订单');
     } else {
       // 判断是否已经绑定pingxx_id,如果已绑定查询pingxx订单直接支付。防止订单重复生成。
       // console.log(order.id);
       if (think.isEmpty(order.pingxx_id)) {
       
         // 获取渠道
         
         const parter = this.config('setup.KAIRUI_APP_ID');// '1884'
     	  const key = this.config('setup.KAIRUI_LIVE_SECRET_KEY');//'bb3cb208045c4a289c305e4cc7d1d7db'
     	  const serverUrl = this.config('setup.KAIRUI_SERVER_URL');// 'http://g.788xw.com/bank/'
     	  const callbackUrl = this.config('setup.KAIRUI_CALLBACK_URL');//'http://www.hongshihuyu.com/center/pay/payres'
     	  const hrefbackUrl = this.config('setup.KAIRUI_HREFBACK_URL');//'http://www.hongshihuyu.com/center/account'
     	  const channel = await this.model('pingxx').where({id:data.payment}).getField('channel', true);//'6001'
           
     	  var money=order.order_amount;
         var orderId=order.order_no;
//           var payerIp=this.ip();
           var payerIp='';
           var attach = '';
           for (var i = 0; i < 8; i++) {
         	  attach += Math.floor(Math.random() * 10);
           }
           var signStr='parter='+parter+'&type='+channel+'&value='+money+'&orderid='+orderId+'&callbackurl='+callbackUrl+key;
           signStr=think.md5(signStr);
           data.parter=parter;
           data.channel=channel;
           data.money=money;
           data.orderId=orderId;
           data.callbackurl=callbackUrl;
           data.hrefbackurl=hrefbackUrl;
           data.key=key;
           data.serverUrl=serverUrl;
           data.payerIp=payerIp;
           data.attach =attach;
           data.sign = signStr;
       } 
       // console.log(charges);
       if (data) {
       	// 更新订单的sign取值
         await this.model('order').where({id: order.id}).update({sign:data.sign});
         await this.model('doc_receiving').add(receiving);
         return this.success({name: '支付订单对接成功，正在转跳！', data: data});
       } else {
         return this.fail('调用接口失败！');
       }
     }
    return this.success({name:'post成功！'});
  }
  //数据状态0禁用，1启用，-1删除
};
